package top.hudk.sql.strategy;

import lombok.extern.slf4j.Slf4j;
import top.hudk.sql.domin.ShardingResult;
import top.hudk.sql.strategy.abstracts.TimeShardingStrategy;

import java.util.Calendar;
import java.util.Date;

/**
 * 请输入描述信息
 *
 * @author HuDongkang
 * @date 2022/9/1 16:05
 */
@Slf4j
public class WeekShardingStrategy extends TimeShardingStrategy {

    /**
     * 分库分表策略
     *
     * @param timeValue   分表字段的值
     * @return 分库分表结果
     */
    @Override
    public ShardingResult sharding(Date timeValue) {
        if (timeValue == null) {
            throw new IllegalArgumentException("invalid shardingValue");
        } else {
            Calendar c = Calendar.getInstance();
            c.setFirstDayOfWeek(Calendar.MONDAY);
            c.setTime(timeValue);
            String tableKey = String.format("%sw%s", c.get(Calendar.YEAR), c.get(Calendar.WEEK_OF_YEAR));
            return new ShardingResult("", tableKey);
        }
    }
}
